//
//  BlueToothSendData.m
//  BluetoothProject
//
//  Created by JianWei Chen on 16/9/5.
//  Copyright © 2016年 JianWei Chen. All rights reserved.
//

#import "DTCBlueToothSendData.h"
#import "DTCBluetooth.h"

#define SendDataTime 0.3       //指令发送间隔时间长度

//#define kLinkServiceUUID          @"6E400001-B5A3-F393-E0A9-E50E24DCCA9E"
#define WriteCharacteristicValue @"FAB1"
//#define NotifyCharacteristicValue @"0003CBB1-0000-1000-8000-00805F9B0131"
//#define FilterServiceValue @"8F400801-CFB4-14A3-F1BA-F61F35CDDBAF"
//#define ReadCharacteristicValue @""



@interface DTCBlueToothSendData ()

@property (strong, nonatomic) DTCDeviceModel *currentModel;
@property (strong, nonatomic) NSDate *currentDate;
@property (strong, nonatomic) NSTimer *actionTimer;
@property (strong, nonatomic) NSMutableArray *actionArray;

@end

@implementation DTCBlueToothSendData

#pragma mark - 初始化
+ (DTCBlueToothSendData *)sharedInstance
{
    static DTCBlueToothSendData *static_manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        static_manager = [[DTCBlueToothSendData alloc]init];
    });
    return static_manager;
}

- (instancetype)init
{
    if ([super init]) {
        [self actionTimer];
    }
    return self;
}

- (NSMutableArray *)actionArray
{
    if (!_actionArray) {
        _actionArray = [[NSMutableArray alloc]init];
    }
    return _actionArray;
}

- (NSTimer *)actionTimer
{
    if (!_actionTimer) {
        _actionTimer = [NSTimer scheduledTimerWithTimeInterval:SendDataTime target:self selector:@selector(doAction:) userInfo:nil repeats:YES];
    }
    return _actionTimer;
}

#pragma mark - 事件处理
#pragma mark - 按时间间隔执行事件
- (void)addCirculateDoAction:(BlueToothAction)action
{
    [self.actionArray addObject:action];
}



- (void)doAction:(NSTimer *)timer
{
    if (self.actionArray.count != 0) {
        BlueToothAction action = self.actionArray[0];
        action();
        [self.actionArray removeObject:action];
    }
}

- (void)sendDataByData:(NSData *)data andInfo:(DTCDeviceModel *)info
{
    static  int i = 0;
    BlueToothAction block = ^{
        if (info == nil) {
            return;
        }
        i++;
        
        self.currentModel = info;
        if (info.currentPeripheral.state == CBPeripheralStateConnected) {
            [[DTCBluetoothManager shareInstance]writeValueToDevice:info writeData:data characteristicValue:WriteCharacteristicValue];
        }
    };
    [self addCirculateDoAction:block];
    
}

#pragma mark - 常见crc校验方法集合

- (NSString *)getCRCValue:(NSString *)hexString
{
    long sum = 0;
    for (int i = 0; i < hexString.length; i += 2) {
        sum += (long)strtoul([[hexString substringWithRange:NSMakeRange(i, 2)]UTF8String], 0, 16);
    }
    NSString *crcString = [NSString stringWithFormat:@"%.10lX",sum];
    return [crcString substringFromIndex:crcString.length - 2];
}

uint16_t CRC16(uint8_t *p, int len)
{
    uint8_t l, h, t;
    int i;
    l = h = 0xff;
    for(i = 0; i < len; i++ )
    {
        h ^= p[i];
        h ^= h>>4;
        t = h;
        h = l;
        l = t;
        t = (l << 4) | (l >> 4);
        h ^= ((t << 2) | (t >> 6)) & 0x1f;
        h ^= t & 0xf0;
        l ^= ((t << 1) | (t >> 7)) & 0xe0;
    }
    return ((uint16_t)h << 8) | l;
}

#pragma mark - 指令书写处

@end
